package org.jeecg.modules.app.user.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.ApiLogin;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.app.user.vo.AppUser;
import org.jeecg.modules.app.user.vo.ResultUser;
import org.jeecg.modules.app.util.check.CheckUtil;
import org.jeecg.modules.power.util.userutil.MemberUserUtil;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @BelongsProject: jeecg-boot-parent
 * @BelongsPackage: org.jeecg.modules.app.user.controller
 * @Author: mi
 * @CreateTime: 2024-07-12 14:39
 * @Description:
 */
@CrossOrigin
@Api(tags = "APP运营端用户相关接口")
@RestController
@RequestMapping("/api/appUser")
public class AppUserController {

    @Autowired
    private ISysUserService sysUserService;


    @Autowired
    private MemberUserUtil memberUserUtil;//任务


    @ApiLogin
    @ApiOperation("APP运营端获取用户信息")
    @GetMapping("/getAppUserEnt")
    public Result<AppUser> getAppUserEnt(HttpServletRequest request){
        SysUser user=new ObjectMapper().convertValue(request.getAttribute("sysUser"),SysUser.class);//转对象 用户登陆对象
        AppUser appUser=new AppUser();
        appUser.setId(user.getId());
        appUser.setUsername(user.getUsername());
        appUser.setRealname(user.getRealname());
        appUser.setGroupId(user.getGroupIds());
        appUser.setIsLeader(user.getIsLeaders());
        appUser.setGroupName(user.getGroupName());
//        appUser.setDepartment("业务部");
        appUser.setAvatar(oConvertUtils.isEmpty(user.getAvatar()) ?"": user.getAvatar());
        Result<AppUser> result = new Result<AppUser>();
        result.setCode(200);
        result.setResult(appUser);
        return result;
    }



    //修改密码

    @ApiLogin
    @ApiOperation("APP运营端修改用户密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "password", value = "新密码", required = true),
            @ApiImplicitParam(name = "confirmPassword", value = "确认密码", required = true),
    })
    @PostMapping("/editPassword")
    public Result<?> editPassword(HttpServletRequest request,String password,String confirmPassword){

        Integer i= CheckUtil.checkPassword(password);
        if (i==1){
            return Result.error_599("密码长度不规范,应为6至20位");
        }

        if (oConvertUtils.isEmpty(password)){
            return Result.error_599("请输入新密码");
        }
        if (oConvertUtils.isEmpty(confirmPassword)){
            return Result.error_599("请输入新密码");
        }
        if (!confirmPassword.equals(password)){
            return Result.error_599("两次输入不符");
        }
        SysUser user=new ObjectMapper().convertValue(request.getAttribute("sysUser"),SysUser.class);//转对象 用户登陆对象
        SysUser sysUser=new SysUser();
        sysUser.setId(user.getId());
        String salt = oConvertUtils.randomGen(8);
        sysUser.setSalt(salt);
        String passwordEncode = PasswordUtil.encrypt(user.getUsername(), password, salt);
        sysUser.setPassword(passwordEncode);
        sysUserService.updateById(sysUser);
        return Result.OK("修改成功");

    }











    //处理任务
    @PostMapping("/openSecondActivity")
    public Result<?> openSecondActivity(String key){
        if (key.equals("openSecondActivity123456")){
            memberUserUtil.startMemberUserTask();
        }
        return Result.OK();
    }






}
